﻿

using System;
using System.Collections.Generic;
using System.Data;
using BusinessObjects.BussinessDictionary;
using DataObjects.Shared;
using DataObjects.StandartInterfaces.Dictionary;

namespace DataObjects.ADO.SqlServer.Dictionary
{

    /// <summary>
    /// 
    /// </summary>
    public class SqlServerAutoNumberDao : IAutoNumberDao 
    {
        /// <summary>
        /// Takes the specified employ test.
        /// </summary>
        /// <param name="autoNumber">The employ test.</param>
        /// <returns></returns>
        private static object[] Take(AutoNumber autoNumber)
        {
            return new object[]  
            {
                 "@AutoID", autoNumber.AutoId,

                  "@RefType", autoNumber.RefTypeId,
                "@Prefix", autoNumber.Prefix,
                "@Suffix", autoNumber.Suffix,
                "@Number", autoNumber.Value,
                //"@LengthOfValue", autoNumber.LengthOfValue,
                  "@UnitID" , autoNumber.UnitId
            };
        }
        /// <summary>
        /// The make
        /// </summary>
        private static readonly Func<IDataReader, AutoNumber> Make = reader =>
          new AutoNumber
          {
              AutoId = reader["AutoID"].AsInt(),
              RefTypeId = reader["RefType"].AsInt(),
              Prefix = reader["Prefix"].AsString(),
              Value = reader["Number"].AsInt(),
              //LengthOfValue = reader["LengthOfValue"].AsInt(),
              Suffix = reader["Suffix"].AsString(),
              UnitId = reader["UnitID"].AsLong()
          };

        /// <summary>
        /// Gets the paged.
        /// </summary>
        /// <param name="unitId">The unit identifier.</param>
        /// <param name="currentPage">The current page.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public List<AutoNumber> GetPaged(int unitId, int currentPage)
        {
            const string sql = @"uspGetPaged_AutoNumber";
            object[] parms = { "@WhereClause", null, "@OrderBy", null, "@PageIndex", currentPage - 1, "@UnitID", unitId };
            return Db.ReadList(sql, CommandType.StoredProcedure, Make, parms);
        }

        /// <summary>
        /// </summary>
        /// <param name="unitId"></param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public int GetPageSize(int unitId)
        {
            const string sql = @"uspGetPageSize_AutoNumber";
            object[] parms = { "@WhereClause", null, "@UnitID", unitId };
            return Db.GetCount(sql, CommandType.StoredProcedure, parms);
        }

       

       

        /// <summary>
        /// Inserts the autoNumber.
        /// </summary>
        /// <param name="autoNumber">The autoNumber.</param>
        /// <returns></returns>
        public string InsertAutoNumber(AutoNumber autoNumber)
        {
            const string sql = "uspInsert_AutoNumber";
            return Db.Insert(sql, CommandType.StoredProcedure, Take(autoNumber));
        }

        /// <summary>
        /// Updates the autoNumber.
        /// </summary>
        /// <param name="autoNumber">The autoNumber.</param>
        /// <returns></returns>
        public string UpdateAutoNumber(AutoNumber autoNumber)
        {
            const string sql = "uspUpdate_AutoNumber";
            return Db.Update(sql, CommandType.StoredProcedure, Take(autoNumber));
        }

        /// <summary>
        /// Deletes the autoNumber.
        /// </summary>
        /// <param name="autoNumber">The autoNumber.</param>
        /// <returns></returns>
        public string DeleteAutoNumber(AutoNumber autoNumber)
        {
            const string sql = @"uspDelete_AutoNumber";
            object[] parms = { "@EmployID", autoNumber.UnitId };
            return Db.Delete(sql, CommandType.StoredProcedure, parms);
        }

        /// <summary>
        /// Gets the autoNumbers by unit.
        /// </summary>
        /// <param name="autoNumberId">The unit identifier.</param>
        /// <returns></returns>
        public AutoNumber GetAutoNumber(int autoNumberId)
        {
            const string sql = @"uspGet_AutoNumber_ByID";
            object[] parms = { "@EmployID", autoNumberId };
            return Db.Read(sql, CommandType.StoredProcedure,Make, parms);
        }


        public AutoNumber GetAutoNumberByRefType(int unitId, int refType)
        {
            const string procedures = @"uspGet_AutoNumber_ByRefType";

            object[] parms = { "@RefType", refType ,
                             "@UnitID", unitId };
            return Db.Read(procedures, CommandType.StoredProcedure, Make, parms);
        }
    }
}
